<?php

// ##############################################################################||
// #                                                                 
// #   MySmartBB Version 1.7.0	                                      
// #   http://www.MySmartBB.com                                      
// #   Copyright (c) 2008 by MySmartBB team                           
// #   license http://opensource.org/licenses/gpl-license.php GNU Public License
// #                                                             
// #   filename : index.php                                            
// #   the index page                             
// #                                                                  
// ##############################################################################||

// ** General definitions **

     define('NOT_IN_INDEX',0);
     include('common.php');
	 include('sb.php');
	 include("includes/spiders.php");
	 

     $SF->html_title_page($info_row['title'] . ' - (Powered By MySmartBB Universal)');

     $Smarty->assign('lastuser_name',$info_row['last_member']);
     $Smarty->assign('lastuser_id',$info_row['last_member_id']);
     $Smarty->assign('member_number',$info_row['member_number']);
     $Smarty->assign('subject_number',$info_row['subject_number']);
     $Smarty->assign('reply_number',$info_row['reply_number']);
	 
	 
		if ($_SERVER['QUERY_STRING'] != "") 
		{
			$url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
		}
		else 
		{
			$url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];	
		}
		
		$spiders     = $DB->sql_query("SELECT * FROM " . $db_prefix . "spider WHERE url = '$url' ORDER BY DATE DESC LIMIT 1");
		$spider      = $DB->sql_num_rows($spiders);
		
         	$Smarty->assign('spider',$spider);
		
		 if ($spider != "")
         {
             $spiders_row = array();
             while ($row = $DB->sql_fetch_array($spiders))
             {
                 $spiders_row[] = $row;
                 $Smarty->assign('spiders_row',$spiders_row);
             }
         }

             // ** This is the announcement section **

         $announcement_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "announcement WHERE id!='0'");
         $announcement_num   = $DB->sql_num_rows($announcement_query);

         	$Smarty->assign('announcement_num',$announcement_num);

         if ($announcement_num != 0)
         {
             $announcement_row = array();
             while ($row = $DB->sql_fetch_array($announcement_query))
             {
                 $announcement_row[] = $row;
                 $Smarty->assign('announcement_row',$announcement_row);
             }
         }
		 
             // ** The end of the announcement section **
			 

			$sql = $DB->sql_query("select id,sitename,sitelink from " .$db_prefix . "textads order by rand() limit 5");
				while($row=$DB->sql_fetch_array($sql))
					{
						$gh[]=$row;
						$Smarty->assign('gh',$gh); 
					}

			 
			 
         $getcards_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "cards WHERE card_member='" . $member_row['username'] . "'");
         $getcards_num = $DB->sql_num_rows($getcards_query);
         $getcards_row = $DB->sql_fetch_array($getcards_query);
		 

             if ($getcards_num != 0)
             {
					if($getcards_row['card_black'] != ""){
					$SF->error('لا يمكنك تصفح المنتدى');  }   
             }
	 
			 
			 
		 
         $admin_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "sectionadmin WHERE section_id='$id'");
         $admin_num   = $DB->sql_num_rows($admin_query);

         $Smarty->assign('admin_num',$admin_num);

         $admin_rows = array();
         while ($admin_row = $DB->sql_fetch_array($admin_query))
         {
             $admin_rows[] = $admin_row;
             $Smarty->assign('admin_rows',$admin_rows);
         }		 
		 		 
     $Smarty->display('homepage_top.tpl');
	
//****

     $forums                 =         array();
     $SectionGroup         =         array();
     $username_style =   array();

     eval($info_row['forums_cache']);
     eval($info_row['sectiongroup_cache']);

     $forums_num         =         count($forums);
     $usergroup_num        =        count($SectionGroup);

     for ($i = 0 ; $i <= $forums_num-1 ; $i++)
     {
         for ($x = 0 ; $x <= $usergroup_num ; $x++)
         {
             // ** This is a main section **
             if ($forums[$i]['main_section'] == 1)
             {
                 if ($SectionGroup[$x]['section_id'] == $forums[$i]['id'])
                 {
                     if ($SectionGroup[$x]['group_id'] == $visitor_row['usergroup'])
                     {
                         if ($SectionGroup[$x]['view_section'] == 1)
                         {
                             $Smarty->assign('main_title',$forums[$i]['title']);
                             $Smarty->display('homepage_sections1.tpl');
                         }
                     }
                 }
             }

             // ** This is not main section **
             else
             {
                 if ($SectionGroup[$x]['section_id'] == $forums[$i]['id'])
                 {
                     if ($SectionGroup[$x]['group_id'] == $visitor_row['usergroup'])
                     {
                         if ($SectionGroup[$x]['view_section'] == 1)
                         {	
							 $subforums_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "section WHERE sub_section='1' AND from_sub_section='".$SectionGroup[$x]['section_id']."' Order By sort ASC");
							 $subforums_num = $DB->sql_num_rows($subforums_query);
							 $Smarty->assign("subforums_num" , $subforums_num);
							 $subforums_rows = array();
							 while($subforums_row = $DB->sql_fetch_array($subforums_query))
							 {
								$subforums_rows[] = $subforums_row;
								$Smarty->assign("subforums_rows" , $subforums_rows);
						  	 }			

                             $section_rowsection_admin = explode(",",$forums[$i][section_admin]);
                             unset($section_rowsection_admin[count($section_rowsection_admin)-1]);
                             $Smarty->assign_by_ref('section_rowsection_admin',$section_rowsection_admin);
							 
							 $last_cut=$SF->titlecut($forums[$i]['last_subject'],20);
							 $last_cut=$SF->SafeOutPuts($last_cut);
							 $Smarty->assign_by_ref('last_cut',$last_cut);
							 
                             $Smarty->assign_by_ref('section_row',$forums[$i]);
                             if ($member_permission == 1)
                             {
                                         $lastreplyicon_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE wr_date>='" . $_COOKIE['MySBB_lastvisit'] . "' AND section='" . $forums[$i]['id'] . "' AND delete_topic<>'1'");
                                         $last_replyicon_num  = $DB->sql_num_rows($lastreplyicon_query);
                             }
$visitors_numb=$DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE notinindex_id='" . $forums[$i]['id'] . "'"));
$Smarty->assign_by_ref('visitors_numb',$visitors_numb);
                             $Smarty->assign_by_ref('last_replyicon_num',$last_replyicon_num);

                             if ($forums[$i]['linksection'] != 1)
                             {
                                 $Smarty->display('homepage_sections2.tpl');

                             }
                             else
                             {
                                 $Smarty->display('homepage_sections-link.tpl');
                             }
                         }
                     }
                 }
             }
         }
     }

//****

     if ($groupper_row['show_hidden'] != 1)
     {
         if ($info_row['show_onlineguest'] != 1)
         {
             $online_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE username<>'زائر' AND user_id<>'' AND hide_browse<>'1' ORDER BY username ASC");
         }

         if ($info_row['show_onlineguest'] == 1)
         {
             $online_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE hide_browse<>'1' ORDER BY username ASC");
         }
     }

     if ($groupper_row['show_hidden'] == 1)
     {
         if ($info_row['show_onlineguest'] != 1)
         {
             $online_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE username<>'زائر' AND username<>'' ORDER BY user_id ASC");
         }

         if ($info_row['show_onlineguest'] == 1)
         {
             $online_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "online ORDER BY username ASC");
         }
     }

     $online_num = $DB->sql_num_rows($online_query);

     $online_row = array();
     while ($online_row2 = $DB->sql_fetch_array($online_query))
     {
         $online_row[] = $online_row2;
         $Smarty->assign('online_row',$online_row);
     }

     if ($groupper_row['show_hidden'] != 1)
     {
         $todayshow_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "today WHERE user_date='".$date."' AND hide_browse<>'1' ORDER BY username ASC");
     }

     if ($groupper_row['show_hidden'] == 1)
     {
         $todayshow_query = $DB->sql_query("SELECT * FROM " . $db_prefix . "today WHERE user_date='".$date."' ORDER BY username ASC");
     }

     $todayshow_row = array();
     while ($todayshow_row2 = $DB->sql_fetch_array($todayshow_query))
     {
         $todayshow_row[] = $todayshow_row2;
         $Smarty->assign('todayshow_row',$todayshow_row);
     }

     $Guest_number   = $DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE username='زائر'"));
     $Member_number  = $DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "online WHERE username<>'زائر'"));
     $Today_number   = $DB->sql_num_rows($DB->sql_query("SELECT * FROM " . $db_prefix . "today WHERE user_date='$date'"));

     $GetMemberStyle = $DB->sql_query("SELECT * FROM " . $db_prefix . "group WHERE view_usernamestyle='1' ORDER BY group_order ASC");

     while ($GetMemberStyleRow = $DB->sql_fetch_array($GetMemberStyle))
     {
         $username_style[] = str_replace('[username]',$GetMemberStyleRow['title'],$GetMemberStyleRow['username_style']);
         $Smarty->assign('username_style',$username_style);
     }

     $Smarty->assign('online_num',$online_num);
     $Smarty->assign('Guest_number',$Guest_number);
     $Smarty->assign('Member_number',$Member_number);
     $Smarty->assign('Today_number',$Today_number);
     $Smarty->assign('todaysubject',$todaysubject);
		 
#------------------------------------------------
		 $d  = date("j/n");
		 $Smarty->assign('d',$d);
#------------------------------------------------
		 $rec_query = $DB->sql_query("SELECT * FROM `" . $db_prefix . "member` WHERE bday ='" . $d . "'");
		 $rec_num	= $DB->sql_num_rows($rec_query);
			
			 $Smarty->assign('rec_num',$rec_num);
			
			 $BD = array();
			 while($row = $DB->sql_fetch_array($rec_query))
				 {
					 $BD[] = $row;
					 $Smarty->assign_by_ref('MBD', $BD);
					 $Smarty->assign('MBD', $BD);
				 }
#------------------------------------------------		 
	 
             // ** lastes posts section **
            $subject_qry = $DB->sql_query("SELECT * FROM " . $db_prefix . "subject WHERE $sentence delete_topic != '1' AND sec_subject != '1' ORDER BY write_time DESC LIMIT 0,5");
            $subject_row = array();
            while($subject = $DB->sql_fetch_array($subject_qry))
            {
                    $subject_row[] = $subject;

                    $Smarty->assign('subject_row', $subject_row);
            }
             // ** The end of the lastes posts section **	 

	if ( $online_num >= $info_row[ 'Highest_Number' ] )
		{
			$DB->sql_query( "UPDATE `" . $db_prefix . "info` SET `Highest_Number` = '" . $online_num . "',`Highest_DATE` = '" . time() . "'" );
		}
		
	 $Highest_Date = date( "d/m/Y h:i a", $info_row[ 'Highest_DATE' ] );
	 $Smarty->assign( 'Highest_Date', $Highest_Date );

     $Smarty->display('homepage_bottom.tpl');
     $Smarty->display('footer.tpl');

/************************************************/ 

?>